// lcs(Longest Common Subsequence)最长公共子序列,不要求连续
// 最长公共子串(要求连续)
function lcs(wordX, wordY, m = wordX.length, n = wordY.length) {
  if (m === 0 || n === 0) {
    return 0;
  }
  // 如果wordX和wordY最后一个值相同，进行递归计算
  if (wordX[m - 1] === wordY[n - 1]) {
    console.log(wordX[m - 1]);
    return 1 + lcs(wordX, wordY, m - 1, n - 1);
  }
  // 最后一个值不相同的情况下，减小索引，向内依次进行对比查询
  const a = lcs(wordX, wordY, m, n - 1);
  console.log(a, "a--");  // wordX不变，和wordY对比的结果
  const b = lcs(wordX, wordY, m - 1, n);
  console.log(b, "--b");  // wordY不变，和wordX对比
  return a > b ? a : b;
}

const wordX = ["a", "c", "b", "a", "e", "d"];
const wordY = ["a", "b", "c", "a", "d", "f"];
console.log(lcs(wordX, wordY)); // 4 【a-b-a-d】
